﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DAL;
namespace CustomProviders
{
    /// <summary>
    /// Summary description for BrandSiteMapProvider
    /// </summary>
    public class BrandSiteMapProvider : StaticSiteMapProvider
    {
        private static SiteMapNode _CurrentSiteMapNode;

        public static SiteMapNode CurrentSiteMapNode
        {
            get 
            { 
                return BrandSiteMapProvider._CurrentSiteMapNode; 
            }
            set { BrandSiteMapProvider._CurrentSiteMapNode = value; }
        }

        private SiteMapNode SetParent(Category[] categories, int i)
        {
            if (i >= categories.Count())
                return null;
            SiteMapNode node = null;
            if (i == 0 && categories.Count() > 1)
            {
                //TODO We need to avoid hardcoded strings
                string url = (categories[i].Url == null) ? string.Format("~/Product.aspx?id={0}", categories[i].CategoryId) : categories[i].Url.UrlValue;
                node = new SiteMapNode(this, categories[i].CategoryId.ToString(), url, categories[i].Title);
            }
            else
            {
                //TODO We need to avoid hardcoded strings
                string url = (categories[i].Url == null) ? string.Format("~/Brand.aspx?id={0}", categories[i].CategoryId) : categories[i].Url.UrlValue;
                node = new SiteMapNode(this, categories[i].CategoryId.ToString(),
                    url,
                    categories[i].Title);
            }
            node.Description = categories[i].Description;
            node.ParentNode = SetParent(categories, ++i);
            return node;
        }
        public override SiteMapNode FindSiteMapNode(HttpContext context)
        {
            string hnd = context.CurrentHandler.ToString();
            int brandId = 0;
            int productId = 0;
            if (hnd.ToUpper() == "ASP.PRODUCT_ASPX")
                productId = Convert.ToInt32(context.Request.QueryString["id"]);
            if (hnd.ToUpper() == "ASP.BRAND_ASPX")
                brandId = Convert.ToInt32(context.Request.QueryString["id"]);
            if (brandId == 0 && productId == 0)
                return null;
            using (EShopDBDataContext db = new EShopDBDataContext())
            {
                Category[] categories = null;
                var siteMapPath = db.GetBrandSiteMapPath(brandId, productId);
                categories = siteMapPath.ToArray();
                if (categories == null)
                    return null;
                _CurrentSiteMapNode = SetParent(categories, 0);
                return _CurrentSiteMapNode;
            }
        }
        public override SiteMapNode BuildSiteMap()
        {
            return null;
        }

        protected override SiteMapNode GetRootNodeCore()
        {
            return null;
        }
    }
}